package cn.chuanw.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.chuanw.wx.annotation.RequiresPermissionsDesc;
import cn.chuanw.core.util.RandomNumberUtil;
import cn.chuanw.core.util.ResponseUtil;
import cn.chuanw.db.dao.WHouseInfoMapper;
import cn.chuanw.db.domain.WHouseInfo;
import cn.chuanw.db.domain.WRoomInfo;
import cn.chuanw.db.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/room")
@Validated
public class RoomController {
    private final Log logger = LogFactory.getLog(RoomController.class);

    @Autowired
    private RoomService roomService;

    @Resource
    private WHouseInfoMapper wHouseInfoMapper;

    @RequiresPermissions("admin:room:list")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房间信息"}, button = "查询")
    @GetMapping("/list")
    public Object list(String roomFullName, String houseId, Integer leaseStatus,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        List<WRoomInfo> wRoomInfos = roomService.querySelective(roomFullName, houseId, leaseStatus, page, limit);
        return ResponseUtil.okList(wRoomInfos);
    }


    @RequiresPermissions("admaddin:room:create")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房间信息"}, button = "新增")
    @PostMapping("/create")
    public Object create(@RequestBody WRoomInfo wRoomInfo) {
        wRoomInfo.setCreateTime(LocalDateTime.now());
        wRoomInfo.setUpdateTime(LocalDateTime.now());
        wRoomInfo.setId(RandomNumberUtil.getStringRandom(12));
        String houseId = wRoomInfo.getHouseId();
        WHouseInfo wHouseInfo = wHouseInfoMapper.selectByPrimaryKey(houseId);
        if (StringUtils.isEmpty(wRoomInfo.getRoomFullName()) && wHouseInfo != null && !StringUtils.isEmpty(wHouseInfo.getFullName())) {
            wRoomInfo.setRoomFullName(wHouseInfo.getFullName() + wRoomInfo.getRoomName());
        } else if (!StringUtils.isEmpty(wRoomInfo.getRoomFullName())) {

        } else {
            wRoomInfo.setRoomFullName(wRoomInfo.getRoomName());
        }
        roomService.addRoomInfo(wRoomInfo);
        return ResponseUtil.ok(wRoomInfo);
    }


    @RequiresPermissions("admaddin:room:update")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房间信息"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody WRoomInfo wRoomInfo) {
        wRoomInfo.setUpdateTime(LocalDateTime.now());
        String houseId = wRoomInfo.getHouseId();
        WHouseInfo wHouseInfo = wHouseInfoMapper.selectByPrimaryKey(houseId);
        if (StringUtils.isEmpty(wRoomInfo.getRoomFullName()) && wHouseInfo != null && !StringUtils.isEmpty(wHouseInfo.getFullName())) {
            wRoomInfo.setRoomFullName(wHouseInfo.getFullName() + wRoomInfo.getRoomName());
        } else if (!StringUtils.isEmpty(wRoomInfo.getRoomFullName())) {

        } else {
            wRoomInfo.setRoomFullName(wRoomInfo.getRoomName());
        }
        roomService.updateById(wRoomInfo);
        return ResponseUtil.ok(wRoomInfo);
    }


    @GetMapping("/options")
    public Object options(Integer leaseStatus) {
        List<WRoomInfo> wRoomInfos = roomService.queryRoomByLeaseStatus(leaseStatus);

        List<Map<String, Object>> options = new ArrayList<>(wRoomInfos.size());
        for (WRoomInfo wRoomInfo : wRoomInfos) {
            Map<String, Object> option = new HashMap<>(2);
            option.put("value", wRoomInfo.getId());
            option.put("label", wRoomInfo.getRoomFullName());
            options.add(option);
        }
        return ResponseUtil.okList(options);
    }

    @GetMapping("/roomRentOptions")
    public Object roomRentOptions() {
        List<WRoomInfo> wRoomInfos = roomService.queryRoom();
        List<Map<String, Object>> options = new ArrayList<>(wRoomInfos.size());
        for (WRoomInfo wRoomInfo : wRoomInfos) {
            Map<String, Object> option = new HashMap<>(2);
            option.put("value", wRoomInfo.getId());
            option.put("label", wRoomInfo.getRoomRent());
            options.add(option);
        }
        return ResponseUtil.okList(options);
    }
}
